﻿@model WebCore.Models.ViewModels.File.Admin._FileManagerViewModel
<link href="@Url.Content("~/Content/themes/base/jquery.fileupload-ui.css")" rel="stylesheet" type="text/css" />
<div class="span-16 file-cover">
    <div class="span-6 file-left" id="filelist">
        @Html.Partial("File/Admin/_FileList", Model)
        
    </div>
    <div class="span-10 last file-right">
        <div class="file-right-cover">
            <div id="file_manager_tab">
                <ul>
                    <li><a href="#tabs-1">@WebCore.Resources.File.PreviewTitle</a></li>
                    <li><a href="#tabs-2">@WebCore.Resources.File.AddNewTitle</a></li>
                    <li><a href="#tabs-3">@WebCore.Resources.File.RenameTitle</a></li>
                </ul>
                <div id="tabs-1">
                    <div class="tab-cover">
                        <div id="file-preview">
                        </div>
                    </div>
                </div>
                <div id="tabs-2">
                    <div class="tab-cover">
                        <div class="section">
                            @WebCore.Resources.File.AddNewDirectoryTitle: <br />
                            <input type="text" id="newFolderName" /><br />
                            <input type="submit" value="@WebCore.Resources.File.AddTitle" id="addNewFolder" />
                        </div>
                        <div class="section">
                            @WebCore.Resources.File.AddNewFileTitle:
                            <form id="file_upload" method="POST" enctype="multipart/form-data">
                            <div class="fileupload-buttonbar">
                                <input type="file" name="files[]" multiple>
                                <div class="progressbar fileupload-progressbar">
                                </div>
                            </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div id="tabs-3">
                    <div class="tab-cover">
                        <div class="section">
                            @WebCore.Resources.File.RenameDirectoryTitle: <br />
                            <input type="text" id="renameFolderName" /><br />
                            <input type="submit" value="@WebCore.Resources.File.RenameTitle" id="renameFolder" />
                        </div>
                        <div class="section">
                            @WebCore.Resources.File.RenameFileTitle: <br />
                            <input type="text" id="renameFileName" /><br />
                            <input type="submit" value="@WebCore.Resources.File.RenameTitle" id="renameFile" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var selectedFilePath;
    var selectedFileName;
    function LoadFolder(rootName, rootPath) {
        $.get('@Url.Action("GetFileListView", "File")?rootName=' + rootName + '&rootPath=' + rootPath, function (data) {
            $('#filelist').html(data);
            $('#renameFolderName').val(currentRootName);
            $('#renameFileName').val('');
            $('#file-preview').html('');
            $('#parentFolder').html(currentParentName);            
            selectedFileName = '';
            selectedFilePath = '';
        });
    }
    function LoadMedia(path, name, divID) {
        $('#file-preview').html('<img src=\"' + path + '\" class="preview-img">'); 
        $('.file-selected').removeClass('file-selected');
        $('#' + divID).addClass('file-selected');
        selectedFilePath = path;
        selectedFileName = name;
        $('#renameFileName').val(selectedFileName);
    }
    function RefreshFileList(newRootName, newRootPath) {
        if (newRootName != null)
            currentRootName = newRootName;
        if (newRootPath != null)
            currentRootPath = newRootPath;

        $.get('@Url.Action("GetFileListView", "File")?rootName=' + currentRootName + '&rootPath=' + currentRootPath, function (data) {
            $('#filelist').html(data);
            $('#renameFileName').val('');
            $('#file-preview').html('');
        });
    }
    function DeleteFolder(name, path) {
        var r = confirm("@WebCore.Resources.File.DeleteFolderConfirmTitle: " + name);
        if (r == true) {
            $.get('@Url.Action("DeleteFolder", "File")?rootPath=' + path, function (data) {
                if (data == "") {
                    alert('@Html.Raw(WebCore.Resources.File.DeleteFolderSuccessTitle)');
                    RefreshFileList();
                }
                else
                    alert(data);
            });
        }
    }
    function DeleteFile(name, path) {
        var r = confirm("@WebCore.Resources.File.DeleteFileConfirmTitle: " + name);
        if (r == true) {
            $.get('@Url.Action("DeleteFile", "File")?rootPath=' + path + '&name=' + name, function (data) {
                if (data == "") {
                    alert('@Html.Raw(WebCore.Resources.File.DeleteFileSuccessTitle)');
                    RefreshFileList();
                } else
                    alert(data);
            });
        }
    }

    $("#file_manager_tab").tabs();

    $('#addNewFolder').click(function () {
        var newFolderName = $('#newFolderName').val();
        $.get('@Url.Action("AddNewFolder", "File")?name=' + newFolderName + '&rootPath=' + currentRootPath, function (data) {
            if (data == "") {
                alert('@Html.Raw(WebCore.Resources.File.AddFolderSuccessTitle)');
                RefreshFileList();
            } else if (data == "EmptyFolderName")
                alert('@Html.Raw(WebCore.Resources.File.ErrorEmptyFolderName)');
            else if (data == "ExistedFolder")
                alert('@Html.Raw(WebCore.Resources.File.ErrorExistedFolderName)');
            else
                alert(data);
        });
    });

    $('#renameFile').click(function () {
        var newFileName = $('#renameFileName').val();
        $.get('@Url.Action("RenameFile", "File")?path=' + currentRootPath + '&oldName=' + selectedFileName + '&newName=' + newFileName, function (data) {
            if (data == "") {
                alert('@Html.Raw(WebCore.Resources.File.RenameFileSuccessTitle)');
                RefreshFileList();
            } else if (data == "EmptyFileName")
                alert('@Html.Raw(WebCore.Resources.File.ErrorEmptyFileName)');
            else
                alert(data);
        });
    });

    $('#renameFolder').click(function () {
        var newFolderName = $('#renameFolderName').val();
        $.get('@Url.Action("RenameFolder", "File")?path=' + currentParentPath + '&oldName=' + currentRootName + '&newName=' + newFolderName, function (data) {
            if (data == "") {
                alert('@Html.Raw(WebCore.Resources.File.RenameFolderSuccessTitle)');
                RefreshFileList(newFolderName, currentParentPath + '/' + newFolderName);
            } else if (data == "EmptyFolderName")
                alert('@Html.Raw(WebCore.Resources.File.ErrorEmptyFolderName)');
            else
                alert(data);
        });
    }); 
</script>
